package com.pine.common.threadpool.config;

import com.pine.common.threadpool.core.DynamicThreadPoolRefresh;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

/**
 * 动态线程池配置
 *
 * @author pine manage
 * @since 2024-08-29
 */
@Slf4j
@ConditionalOnWebApplication
@Configuration
@ConditionalOnProperty(prefix = DynamicThreadPoolProperties.PREFIX, name = "enable", havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(DynamicThreadPoolProperties.class)
public class DynamicThreadPoolConfig {

    @Resource
    private DynamicThreadPoolProperties dynamicThreadPoolProperties;

    @Bean
    @Order(1000)
    public DynamicThreadPoolRefresh dynamicThreadPoolRefresh() {
        DynamicThreadPoolRefresh dynamicThreadPoolRefresh = new DynamicThreadPoolRefresh(dynamicThreadPoolProperties.getScheduleInitialDelay(),
                dynamicThreadPoolProperties.getSchedulePeriod(), dynamicThreadPoolProperties.getScheduleUnit());
        dynamicThreadPoolRefresh.refresh();
        return dynamicThreadPoolRefresh;
    }
}
